package com.punai.config.sqlitedao;

import cn.hutool.core.lang.UUID;
import com.punai.common.exception.base.BaseException;
import com.punai.common.utils.StringUtils;
import com.punai.common.utils.sqlite.SqliteUtils;
import com.punai.config.sqlitedomain.ConfigUnitConver;
import com.punai.config.sqlitedomain.vo.ConfigUnitConverVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Component
public class ConfigUnitConverRepository {
    @Autowired
    private SqliteUtils sqliteUtils;

    public List<ConfigUnitConver> findAllByModel(ConfigUnitConverVo vo) {
        String selSql = "select * from tab_config_conver_coefficient";
        String whereSql = "where 1=1";
        String sql = selSql + " " + whereSql;
        if (vo.getPageNum() != null && vo.getPageSize() != null) {
            int pageNumber = vo.getPageNum();
            int pageSize = vo.getPageSize();
            int start = pageSize * (pageNumber - 1);
            sql += " limit " + start + "," + pageSize;
        }
        return sqliteUtils.selectListRecord(ConfigUnitConver.class, sql);
    }

    public ConfigUnitConver findOneById(String id) {
        String selSql = "select * from tab_config_conver_coefficient";
        String whereSql = "where id='" + id + "'";
        String sql = selSql + " " + whereSql;
        return sqliteUtils.selectOneRecord(ConfigUnitConver.class, sql);
    }
    public int countByModel(ConfigUnitConverVo vo) {
        String selSql = "select count(*) as total from tab_config_conver_coefficient";
        String whereSql = "where 1=1";
        String sql = selSql + " " + whereSql;
        Map unique = sqliteUtils.unique(sql);
        return Integer.parseInt(unique.get("total").toString());
    }

    public ConfigUnitConverVo save(ConfigUnitConverVo vo) {
        if (StringUtils.isNotEmpty(vo.getId())) return this.update(vo);
        String sql = "insert into tab_config_conver_coefficient(" +
                "ID, " +
                "INDEX_UNIT, " +
                "KV_JSON, " +
                "CREATE_DATE, " +
                "EDIT_STATE, " +
                "LAST_MODIFY, " +
                "VERSION, " +
                "SREACH_KEY) values(?,?,?,?,?,?,?,?) ";
        vo.setId(UUID.randomUUID().toString());
        vo.setCreateDate(new Date());
        vo.setLastModify(new Date());
        vo.setSreachKey(vo.toString());
        vo.setEditState(0);
        Object[] args = {
                vo.getId(),
                vo.getIndexUnit(),
                vo.getKvJson(),
                vo.getCreateDate(),
                vo.getEditState(),
                vo.getLastModify(),
                vo.getVersion(),
                vo.getSreachKey(),
        };
        int execute = sqliteUtils.execute(sql, args);
        if (execute == 0) {
            throw new BaseException("ConfigUnitConverRepository.sava失败");
        }
        return vo;
    }

    public ConfigUnitConverVo update(ConfigUnitConverVo vo) {
        String sql = "update tab_config_conver_coefficient set " +
                "INDEX_UNIT = ? , " +
                "KV_JSON = ? , " +
                "LAST_MODIFY = ? , " +
                "SREACH_KEY = ? " +
                "where ID = ?";
        vo.setLastModify(new Date());
        vo.setSreachKey(vo.toString());
        Object[] args = {
                vo.getIndexUnit(),
                vo.getKvJson(),
                vo.getLastModify(),
                vo.getSreachKey(),
                vo.getId()
        };
        int execute = sqliteUtils.execute(sql, args);
        if (execute == 0) {
            throw new BaseException("ConfigUnitConverRepository.update失败");
        }
        return vo;
    }

    public ConfigUnitConver delete(String id) {
        ConfigUnitConver one = this.findOneById(id);
        if (one == null) throw new BaseException("ConfigUnitConverRepository.delete失败");
        String sql = "delete from tab_config_conver_coefficient where id = ?";
        Object[] args = {
                id
        };
        int execute = sqliteUtils.execute(sql, args);
        if (execute == 0) {
            throw new BaseException("ConfigUnitConverRepository.delete失败");
        }
        return one;
    }
}
